package com.infocollect.common.utils.oss;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.*;
import com.infocollect.common.exception.ResponseException;
import com.infocollect.common.utils.AesUtil;
import com.infocollect.framework.config.OSSConfig;
import com.obs.services.ObsClient;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.net.URL;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Random;


public class TYOSSClientUtil {

	/** Logger */
	private static final Logger log = LoggerFactory.getLogger(TYOSSClientUtil.class);

	private ObsClient obsClient;


	private OSSConfig ossConfig;

	public TYOSSClientUtil(OSSConfig ossConfig) {
		this.setOssConfig(ossConfig);
		obsClient = new ObsClient( ossConfig.getAccessKeyId(),
				ossConfig.getAccessKeySecret(),ossConfig.getEndpoint());
	}

	public void setOssConfig(OSSConfig ossConfig) {
		this.ossConfig = ossConfig;
	}

	/**
	 * 销毁
	 */
	public void destory() {
		try{
			obsClient.close();
		}catch(Exception ex)
		{

		}
	}



	/**
	 * 获得签名
	 * 
	 * @throws UnsupportedEncodingException
	 * 
	 */
	public Map<String, String> getSignature(String filedir) throws UnsupportedEncodingException {
//		final String key = RedisKeyConstant.OSS_SIGNATURE + filedir;
		if(!"".equals(filedir)) {
			obsClient.putObject(ossConfig.getBucket(), filedir, new ByteArrayInputStream(new byte[0]));
		}

		Map<String, String> respMap = new LinkedHashMap<String, String>();
		respMap.put("accessid", ossConfig.getAccessKeyId());
		respMap.put("signature", AesUtil.encode(ossConfig.getAccessKeySecret()));
		respMap.put("dir", filedir);
		respMap.put("bucket", ossConfig.getBucket());
		respMap.put("host", ossConfig.getUrlPath());
		respMap.put("endPoint", ossConfig.getEndpoint());
		respMap.put("tyCloudFlag", ossConfig.getTyCloudFlag());

		return respMap;
	}

}
